Trò chơi Angry Birds trong UNITY Engine
31.656 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4
5
6 public class GoSplineCatmullRomSolver : AbstractGoSplineSolver
7 {
8 public GoSplineCatmullRomSolver( List<Vector3> nodes )
9 {
10 _nodes = nodes;
11 }
12
13
14 #region AbstractGoSplineSolver
15
16 // closing a Catmull-Rom spline: http://cl.ly/GOZv
17 public override void closePath()
18 {
19 // first, remove the control points
20 _nodes.RemoveAt( 0 );
21 _nodes.RemoveAt( _nodes.Count - 1 );
22
23 // if the first and last node are not the same add one
24 if( _nodes[0] != _nodes[_nodes.Count - 1] )
25 _nodes.Add( _nodes[0] );
26
27
28 // figure out the distances from node 0 to the first node and the second to last node (remember above
29 // we made the last node equal to the first so node 0 and _nodes.Count - 1 are identical)
30 var distanceToFirstNode = Vector3.Distance( _nodes[0], _nodes[1] );
31 var distanceToLastNode = Vector3.Distance( _nodes[0], _nodes[_nodes.Count - 2] );
32
33
34 // handle the first node. we want to use the distance to the LAST (opposite segment) node to figure out where this control point should be
35 var distanceToFirstTarget = distanceToLastNode / Vector3.Distance( _nodes[1], _nodes[0] );
36 var lastControlNode = ( _nodes[0] + ( _nodes[1] - _nodes[0] ) * distanceToFirstTarget );
37
38
39 // handle the last node. for this one, we want the distance to the first node for the control point but it should
40 // be along the vector to the last node
41 var distanceToLastTarget = distanceToFirstNode / Vector3.Distance( _nodes[_nodes.Count - 2], _nodes[0] );
42 var firstControlNode = ( _nodes[0] + ( _nodes[_nodes.Count - 2] - _nodes[0] ) * distanceToLastTarget );
43
44 _nodes.Insert( 0, firstControlNode );
45 _nodes.Add( lastControlNode );
46 }
47
48
49 public override Vector3 getPoint( float t )
50 {
51 int numSections = _nodes.Count - 3;
52 int currentNode = Mathf.Min( Mathf.FloorToInt( t * (float)numSections ), numSections - 1 );
53 float u = t * (float)numSections - (float)currentNode;
54
55 Vector3 a = _nodes[currentNode];
56 Vector3 b = _nodes[currentNode + 1];
57 Vector3 c = _nodes[currentNode + 2];
58 Vector3 d = _nodes[currentNode + 3];
59
60 return .5f *
61 (
62 ( -a + 3f * b - 3f * c + d ) * ( u * u * u )
63 + ( 2f * a - 5f * b + 4f * c - d ) * ( u * u )
64 + ( -a + c ) * u
65 + 2f * b
66 );
67 }
68
69
70 public override void drawGizmos()
71 {
72 if( _nodes.Count < 2 )
73 return;
74
75 // draw the control points
76 var originalColor = Gizmos.color;
77 Gizmos.color = new Color( 1, 1, 1, 0.3f );
78
79 Gizmos.DrawLine( _nodes[0], _nodes[1] );
80 Gizmos.DrawLine( _nodes[_nodes.Count - 1], _nodes[_nodes.Count - 2] );
81
82 Gizmos.color = originalColor;
83 }
84
85 #endregion
86
87 }
2 using System.Collections;
3 using System.Collections.Generic;
4
5
6 public class GoSplineCatmullRomSolver : AbstractGoSplineSolver
7 {
8 public GoSplineCatmullRomSolver( List<Vector3> nodes )
9 {
10 _nodes = nodes;
11 }
12
13
14 #region AbstractGoSplineSolver
15
16 // closing a Catmull-Rom spline: http://cl.ly/GOZv
17 public override void closePath()
18 {
19 // first, remove the control points
20 _nodes.RemoveAt( 0 );
21 _nodes.RemoveAt( _nodes.Count - 1 );
22
23 // if the first and last node are not the same add one
24 if( _nodes[0] != _nodes[_nodes.Count - 1] )
25 _nodes.Add( _nodes[0] );
26
27
28 // figure out the distances from node 0 to the first node and the second to last node (remember above
29 // we made the last node equal to the first so node 0 and _nodes.Count - 1 are identical)
30 var distanceToFirstNode = Vector3.Distance( _nodes[0], _nodes[1] );
31 var distanceToLastNode = Vector3.Distance( _nodes[0], _nodes[_nodes.Count - 2] );
32
33
34 // handle the first node. we want to use the distance to the LAST (opposite segment) node to figure out where this control point should be
35 var distanceToFirstTarget = distanceToLastNode / Vector3.Distance( _nodes[1], _nodes[0] );
36 var lastControlNode = ( _nodes[0] + ( _nodes[1] - _nodes[0] ) * distanceToFirstTarget );
37
38
39 // handle the last node. for this one, we want the distance to the first node for the control point but it should
40 // be along the vector to the last node
41 var distanceToLastTarget = distanceToFirstNode / Vector3.Distance( _nodes[_nodes.Count - 2], _nodes[0] );
42 var firstControlNode = ( _nodes[0] + ( _nodes[_nodes.Count - 2] - _nodes[0] ) * distanceToLastTarget );
43
44 _nodes.Insert( 0, firstControlNode );
45 _nodes.Add( lastControlNode );
46 }
47
48
49 public override Vector3 getPoint( float t )
50 {
51 int numSections = _nodes.Count - 3;
52 int currentNode = Mathf.Min( Mathf.FloorToInt( t * (float)numSections ), numSections - 1 );
53 float u = t * (float)numSections - (float)currentNode;
54
55 Vector3 a = _nodes[currentNode];
56 Vector3 b = _nodes[currentNode + 1];
57 Vector3 c = _nodes[currentNode + 2];
58 Vector3 d = _nodes[currentNode + 3];
59
60 return .5f *
61 (
62 ( -a + 3f * b - 3f * c + d ) * ( u * u * u )
63 + ( 2f * a - 5f * b + 4f * c - d ) * ( u * u )
64 + ( -a + c ) * u
65 + 2f * b
66 );
67 }
68
69
70 public override void drawGizmos()
71 {
72 if( _nodes.Count < 2 )
73 return;
74
75 // draw the control points
76 var originalColor = Gizmos.color;
77 Gizmos.color = new Color( 1, 1, 1, 0.3f );
78
79 Gizmos.DrawLine( _nodes[0], _nodes[1] );
80 Gizmos.DrawLine( _nodes[_nodes.Count - 1], _nodes[_nodes.Count - 2] );
81
82 Gizmos.color = originalColor;
83 }
84
85 #endregion
86
87 }